package com.li.general.popups
{
import com.li.general.events.PopUpEvent;

import flash.display.Sprite;
import flash.events.EventDispatcher;

public class PopUpManager extends EventDispatcher
{
    private var _container:Sprite;
    private var _activePopUp:PopUp;
    private var _blocker:Sprite;

    public function PopUpManager(popUpContainer:Sprite, blocker:Sprite)
    {
        super();
        _container = popUpContainer;
        _blocker = blocker;
    }

    public function showPopUp(popUp:PopUp):void
    {
        if(_activePopUp)
            return;

        _activePopUp = popUp;
        _activePopUp.addEventListener(PopUpEvent.CLOSE_POP_UP, closePopUpHandler);

        _container.addChild(popUp);

        _blocker.visible = true;
    }

    public function hidePopUp():void
    {
        _container.removeChild(_activePopUp);
        _activePopUp = null;
    }

    private function closePopUpHandler(evt:PopUpEvent):void
    {
        hidePopUp();

        var closeEvent:PopUpEvent = new PopUpEvent(PopUpEvent.POP_UP_CLOSED);
        dispatchEvent(closeEvent);

        _blocker.visible = false;
    }
}
}